import os.path
import sqlite3
from typing import Dict, Tuple

from constant import root_path


class HandianData:

    cursor = None

    @staticmethod
    def init():
        file_name = os.path.join(root_path, 'data/handian/dict.db')
        HandianData.cursor = sqlite3.connect(file_name).cursor()

    wubi_token_cache:Dict[str, str] = dict()
    @staticmethod
    def get_wubi_seq_cached(char:str):
        """
        五笔
        :param char:
        :return:
        """
        if HandianData.cursor is None:
            HandianData.init()
        if char not in HandianData.wubi_token_cache:
            if '\u4e00' <= char <= '\u9fff':
                query = f"select WuBi from Dicts where Char = '{char}'"
                HandianData.cursor.execute(query)
                rows = HandianData.cursor.fetchone()
                if rows[0] is not None:
                    seq = rows[0]
                    HandianData.wubi_token_cache[char] = seq
                else:
                    HandianData.zhengma_token_cache[char] = ''
            else:
                HandianData.wubi_token_cache[char] = ''
        return HandianData.wubi_token_cache[char]
    
    zhengma_token_cache:Dict[str, str] = dict()

    @staticmethod
    def get_zhengma_seq_cached(char:str):
        """
        郑码
        :param char:
        :return:
        """
        if HandianData.cursor is None:
            HandianData.init()
        if char not in HandianData.zhengma_token_cache:
            if '\u4e00' <= char <= '\u9fff':
                query = f"select ZhengMa from Dicts where Char = '{char}'"
                HandianData.cursor.execute(query)
                rows = HandianData.cursor.fetchone()
                if rows[0] is not None:
                    seq = rows[0]
                    HandianData.zhengma_token_cache[char] = seq
                else:
                    HandianData.zhengma_token_cache[char] = ''
            else:
                HandianData.zhengma_token_cache[char] = ''
        return HandianData.zhengma_token_cache[char]


    pinyin_token_cache:Dict[str, str] = dict()

    @staticmethod
    def get_pinyin_seq_cached(char:str):
        """
        拼音
        :param char:
        :return:
        """
        if HandianData.cursor is None:
            HandianData.init()
        if char not in HandianData.pinyin_token_cache:
            if '\u4e00' <= char <= '\u9fff':
                query = f"select PinYin2 from Dicts where Char = '{char}'"
                HandianData.cursor.execute(query)
                rows = HandianData.cursor.fetchone()
                if rows[0] is not None:
                    seq = rows[0]
                    HandianData.pinyin_token_cache[char] = seq
                else:
                    HandianData.pinyin_token_cache[char] = ''
            else:
                HandianData.pinyin_token_cache[char] = ''
        return HandianData.pinyin_token_cache[char]


    bishun_token_cache:Dict[str, str] = dict()

    @staticmethod
    def get_bishun_seq_cached(char:str):
        """
        笔顺
        :param char:
        :return:
        """
        if HandianData.cursor is None:
            HandianData.init()
        if char not in HandianData.pinyin_token_cache:
            if '\u4e00' <= char <= '\u9fff':
                query = f"select BiShun2 from Dicts where Char = '{char}'"
                HandianData.cursor.execute(query)
                rows = HandianData.cursor.fetchone()
                if rows[0] is not None:
                    seq = rows[0]
                    HandianData.bishun_token_cache[char] = seq
                else:
                    HandianData.bishun_token_cache[char] = ''
            else:
                HandianData.bishun_token_cache[char] = ''
        return HandianData.bishun_token_cache[char]


    strokes_token_cache:Dict[Tuple[str, int], str] = dict()
    @staticmethod
    def get_strokes_seq_cached(char:str, max_stroke_num:int = 20):
        if HandianData.cursor is None:
            HandianData.init()
        if (char, max_stroke_num) not in HandianData.strokes_token_cache:
            if '\u4e00' <= char <= '\u9fff':
                query = f"select BiShun2 from Dicts where Char = '{char}'"
                HandianData.cursor.execute(query)
                rows = HandianData.cursor.fetchone()
                if rows[0] is not None:
                    seq = rows[0]
                    HandianData.strokes_token_cache[(char, max_stroke_num)] = seq
                else:
                    HandianData.strokes_token_cache[(char, max_stroke_num)] = ''
            else:
                HandianData.strokes_token_cache[(char, max_stroke_num)] = ''
        return HandianData.strokes_token_cache[(char, max_stroke_num)]